<?php

namespace App\Containers\AppSection\Profile\Tasks;

use App\Containers\AppSection\Profile\Data\Repositories\ProfileRepository;
use App\Containers\AppSection\Profile\Models\Profile;
use App\Ship\Exceptions\CreateResourceFailedException;
use App\Ship\Parents\Tasks\Task;
use Exception;

class CreateProfileTask extends Task
{
    protected ProfileRepository $repository;

    public function __construct(ProfileRepository $repository)
    {
        $this->repository = $repository;
    }

    public function run(array $data): Profile
    {
        try {
            return $this->repository->create(
                array_merge(
                    $data,
                    [
                        'accreditation_status_key' => 'accreditation_not_accredited',
                        'qualification_status_key' => 'qualification_not_qualified',
                        'phone_verified_at' => now(),
                        'accredited_at' => null
                    ]
                )
            );
        } catch (Exception $exception) {
            throw new CreateResourceFailedException();
        }
    }
}
